#include "lib.h"
#include "endian.h"

int32
Endian::ReceiveInt4( int32* pI, uint8* pBuf )
{
	int32 k=0;

	int32 val = 0;

	val |= (pBuf[k++] <<  0);
	val |= (pBuf[k++] <<  8);
	val |= (pBuf[k++] << 16);
	val |= (pBuf[k++] << 24);

	*pI = val;

	return k;
}

int32
Endian::SendInt4( int32 i, uint8* pBuf )
{
	int32 k=0;

	pBuf[k++] = (uint8)((i >> 0) & 0xFF);
	pBuf[k++] = (uint8)((i >> 8) & 0xFF);
	pBuf[k++] = (uint8)((i >>16) & 0xFF);
	pBuf[k++] = (uint8)((i >>24) & 0xFF);

	return k;
}

int32
Endian::ReceiveInt8( int64* pI, uint8* pBuf )
{
	int32 k=0;

	int64 val = 0;

	val |= ((int64)pBuf[k++] <<  0);
	val |= ((int64)pBuf[k++] <<  8);
	val |= ((int64)pBuf[k++] << 16);
	val |= ((int64)pBuf[k++] << 24);
	val |= ((int64)pBuf[k++] << 32);
	val |= ((int64)pBuf[k++] << 40);
	val |= ((int64)pBuf[k++] << 48);
	val |= ((int64)pBuf[k++] << 56);

	*pI = val;

	return k;
}

int32
Endian::SendInt8( int64 i, uint8* pBuf )
{
	int32 k=0;

	pBuf[k++] = (uint8)((i >> 0) & 0xFFFF);
	pBuf[k++] = (uint8)((i >> 8) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>16) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>24) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>32) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>40) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>48) & 0xFFFF);
	pBuf[k++] = (uint8)((i >>54) & 0xFFFF);

	return k;
}

int32
Endian::ReceiveInt2( int16* pI, uint8* pBuf )
{
	int32 k=0;

	int16 val = 0;

	val	|= (pBuf[k++] << 0);
	val	|= (pBuf[k++] << 8);

	*pI = val;

	return k;
}


int32
Endian::ReceiveInt2( uint16* pI, uint8* pBuf )
{
	int32 k=0;

	uint16 val = 0;

	val	|= (pBuf[k++] << 0);
	val	|= (pBuf[k++] << 8);

	*pI = val;

	return k;
}


int32
Endian::SendInt2( int16 i, uint8* pBuf )
{
	int32 k=0;

	pBuf[k++] = (uint8)((i >> 0) & 0xFF);
	pBuf[k++] = (uint8)((i >> 8) & 0xFF);

	return k;
}

int32
Endian::SendInt2( uint16 i, uint8* pBuf )
{
	int32 k=0;

	pBuf[k++] = (uint8)((i >> 0) & 0xFF);
	pBuf[k++] = (uint8)((i >> 8) & 0xFF);

	return k;
}


int32
Endian::ReceiveInt1( int8* pI, uint8* pBuf )
{
	int32 k = 0;

	int8 val = 0;

	val	|= (pBuf[k++] << 0);

	*pI = val;

	return k;
}

int32
Endian::SendInt1( int8 i, uint8* pBuf )
{
	int32 k = 0;

	pBuf[k++] = (uint8)((i >> 0) & 0xFF);

	return k;
}

int32
Endian::ReceiveByte( uint8* pU, uint8* pBuf )
{
	int32 k = 0;

	*pU = pBuf[k++];

	return k;
}

int32 
Endian::SendByte( uint8 u, uint8* pBuf )
{
	int32 k = 0;

	pBuf[k++] = u;

	return k;
}

